package com.srit.system.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.srit.core.bean.Page;
import com.srit.system.bean.Role;
import com.srit.system.bean.Url;
import com.srit.system.dao.RoleDao;

@Service
@Transactional
public class RoleDaoImpl implements RoleDao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public int getMaxId() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void save(Role t) {
		String sql = "insert into TC_ROLE(ID,NAME,URL_ID,enabled) values(SEQ_TC_ROLE.nextval,?,?,?)";
		jdbcTemplate.update(sql,new Object[]{
				t.getName(),t.getUrl_id(),
				t.getEnabled()
		});

	}

	@Override
	public void update(Role t) {
		String sql = "update TC_ROLE set NAME=?,URL_ID=?,ENABLED=? where ID = ?";
		jdbcTemplate.update(sql,new Object[]{
				t.getName(),t.getUrl_id(),
				t.getEnabled(),t.getId()
		});
	}

	@Override
	public Role getById(Integer id) {
		String sql = "select * from TC_ROLE where id =";
		return (Role)jdbcTemplate.queryForObject(sql+id, new RoleRowMapper());
	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void page(Page page) {
		String sqlCount = "select count(*) from TC_ROLE where 1=1 ";
		String sqlAnd = " ";
		String sqlSelect = "select t2.ID,t2.NAME,t2.URL_ID,t2.enabled"
				+ sqlAnd + "from (select rownum r,t1.* from TC_ROLE t1 where rownum<? "
				+ sqlAnd + ") t2 where t2.r>?";
		int count = jdbcTemplate.queryForInt(sqlCount + sqlAnd);
		List<Url> list = jdbcTemplate.query(sqlSelect, new Object[] {
				(page.getPageNum()+1)*page.getPageSize(),page.getPageNum()*page.getPageSize() }, new RoleRowMapper());
		page.setCount(count);
		page.setList(list);

	}
	
	
	@Override
	public Role getRoleByUseId(int userId) {
		String sql = "select t1.ID,t1.NAME,t1.URL_ID,t1.ENABLED from TC_ROLE t1,TC_USERROLE t2 where t1.ID = t2.ROLEID and t2.USERID = ?";
		Role role = jdbcTemplate.queryForObject(sql,new Object[]{userId}, new RoleRowMapper());
		return role;
	}


	class RoleResultSetExtractor implements ResultSetExtractor {
		@Override
		public Object extractData(ResultSet rs) throws SQLException,
				DataAccessException {
			Role role = new Role();
			role.setId(rs.getInt("ID"));
			role.setName(rs.getString("NAME"));
			role.setUrl_id(rs.getString("URL_ID"));
			role.setEnabled(rs.getString("ENABLED"));
			return role;
		}

	}

	class RoleRowMapper implements RowMapper {

		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			RoleResultSetExtractor extractor = new RoleResultSetExtractor();
			return extractor.extractData(rs);
		}

	}

}
